package first.endtoend
/**
 * Created with IntelliJ IDEA.
 * User: Olfa
 * Date: 15/03/13
 * Time: 14:56
 * To change this template use File | Settings | File Templates.
 */
class Address implements Serializable
{


    String numStreet = ""
    String nameStreet = ""
    static belongsTo = [locality: Locality, family: Family]
    String zipCode
    String localityName
    static constraints = {
        numStreet nullable: true
        nameStreet nullable: true
        zipCode nullable: true
        locality(nullable: false)
        family(nullable: true)
        localityName nullable: true

    }

    public Address(String aString)
    {
        int compt = 0
        def c = aString[0]
        def property = new String()

        int occur = 0 // number of occurence of ;


        while (compt < aString.size())
        {
            c = aString[compt]

            if (!c.equals(';'))
            {
                property = property + c
                compt++

            } else if (c.equals(';'))
            {
                occur++

                compt = compt + 2
                property = ''
            }


            switch (occur)
            {
                case 0: numStreet = property
                    break
                case 1: zipCode = property
                    break
                case 2: if (compt == aString.size())
                {
                    localityName = property
                    break
                } else
                {
                    break
                }

            }
        }

        locality = Locality.findByName(localityName)
        this.setLocality(locality)

    }

    @Override
    public String toString()
    {


        return numStreet + ", " + nameStreet + " , " + zipCode + ", " + localityName

    }
}
